package log.aop;

import log.util.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.springframework.util.MultiValueMap;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.support.StandardMultipartHttpServletRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author
 * @Desc
 * @date 2021/6/30 9:27
 */
@Slf4j
public class BaseAspect {
    @Resource
    protected HttpServletRequest request;

    /**
     */
    protected void outputConsoleLogs(JoinPoint point, String storeyName, String inOutType, String param) {
        if (log.isDebugEnabled()) {
            String className = point.getSignature().getDeclaringType().getSimpleName();
            String methodName = point.getSignature().getName();
            if (StringUtils.hasText(param)) {
                log.debug(
                    String.format("%s [%s %s.%s]%nParameters:[%s]", inOutType, storeyName, className, methodName,
                        param));
            } else {
                log.debug(String.format("%s [%s %s.%s]", inOutType, storeyName, className, methodName));
            }
        }
    }

    /**
     * @param point
     * @Desc 获取入参
     */
    protected String getRequestParams(JoinPoint point) {
        // 普通请求入参
        Map<String, String[]> requestMap = new HashMap(request.getParameterMap());
        requestMap.remove("timestamp");
        if (requestMap.size() > 0) {
            return JsonUtils.toJSONString(requestMap);
        }
        Object[] args = point.getArgs();
        if (ObjectUtils.isEmpty(args)) {
            return null;
        }
        StringBuffer params = new StringBuffer();
        Object obj = args[0];
        // 文件请求
        if (obj instanceof StandardMultipartHttpServletRequest) {
            StandardMultipartHttpServletRequest standardMultipartHttpServletRequest =
                (StandardMultipartHttpServletRequest)obj;
            MultiValueMap<String, MultipartFile> multiValueMap = standardMultipartHttpServletRequest.getMultiFileMap();
            Map<String, Object> map = new HashMap<String, Object>(6);
            for (String key : multiValueMap.keySet()) {
                List<Object> fileNameList = new ArrayList<>();
                for (MultipartFile multipartFile : multiValueMap.get(key)) {
                    Map<String, Object> fileMap = new HashMap<String, Object>(2);
                    fileMap.put("name", multipartFile.getOriginalFilename());
                    fileMap.put("size", multipartFile.getSize());
                    fileNameList.add(fileMap);
                }
                map.put(key, fileNameList);
            }
            params.append(JsonUtils.toJSONString(map));
        } else {
            // json body请求
            try {
                String argString = JsonUtils.toJSONString(obj);
                params.append(argString);
            } catch (Exception e) {
                log.error(e.getMessage(), e);
            }
        }
        return params.toString();
    }

}
